package com.java2007.hotel.controller.backend;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.service.impl.DinnerTableServiceImpl;
import com.java2007.hotel.utils.DateUtil;
import jdk.internal.org.objectweb.asm.tree.JumpInsnNode;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
@WebServlet("/dinnertable")
public class DinnerTableController extends Controller {
    private DinnerTableService dinnerTableService = new DinnerTableServiceImpl();

    /**
     * 按名字查询餐桌，如果名字为null则查询所有
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findDinnerTableByName(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String tableName = request.getParameter("tableName");
        List<DinnerTable> dinnerTableList = dinnerTableService.findDinnerTableByName(tableName);
        request.setAttribute("dinnerTableList", dinnerTableList);
        request.setAttribute("tableName", tableName);
        return "forward:/backend/detail/table/table-list.jsp";
    }

    /**
     * 预约，退订餐桌
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String updateDinnerTableStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String tableScheduledTime = request.getParameter("tableScheduledTime");
        String tableIdStr = request.getParameter("tableId");
        String tableStatusStr = request.getParameter("tableStatus");
        //判断值是否正确
        if (tableIdStr.isEmpty() || null == tableIdStr || tableStatusStr.isEmpty() || null == tableStatusStr) {
            return ServletConstant.jump("操作失败", "dinnertable?method=findDinnerTableByName&tableName=");
        }

        //状态餐桌预约判断 0 ：预约 1 ：退桌
        Integer tableStatus = Integer.valueOf(tableStatusStr);
        Integer tableId = Integer.valueOf(tableIdStr);
        //预约操作
        if (tableStatus == 0) {
            //时间判断
            Date date = null;
            if (tableScheduledTime != null && !tableScheduledTime.isEmpty()) {
                //修改前端的时间格式，将T替换为" "
                String replace = tableScheduledTime.replace("T", " ");
                date = DateUtil.strToDate(replace, "yyyy-MM-dd HH:mm");
                Date date1 = new Date();
                //判断时间是否比当前时间少
                if (date.getTime() > date1.getTime()) {
                    String message = dinnerTableService.updateDinnerTableStatus(tableId, date);
                    return ServletConstant.jump(message, "dinnertable?method=findDinnerTableByName&tableName=");
                }
                return ServletConstant.jump("不能预约当前时间之前", "dinnertable?method=findDinnerTableByName&tableName=");
            }
            return ServletConstant.jump("请选择预约时间", "dinnertable?method=findDinnerTableByName&tableName=");
        }
        //退桌操作
        String message = dinnerTableService.updateDinnerTableStatus(tableId, new Date());
        return ServletConstant.jump(message, "dinnertable?method=findDinnerTableByName&tableName=");

    }

    /**
     * 通过tableid删除
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String deleteDinnerTableByTableId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String tableIdStr = request.getParameter("tableId");
        //判断tableId是否为空或null
        if (tableIdStr.isEmpty() || tableIdStr == null) {
            return ServletConstant.jump("删除失败", "dinnertable?method=findDinnerTableByName&tableName=");
        }

        Integer tableId = Integer.valueOf(tableIdStr);
        //查询餐桌是否存在
        DinnerTable dinnerTableByTableId = dinnerTableService.findDinnerTableByTableId(tableId);
        if (dinnerTableByTableId == null){
            //餐桌不存在
            return ServletConstant.jump("删除失败","dinnertable?method=findDinnerTableByName&tableName=");
        }
        //餐桌存在
        //判断餐桌是否被预定，预定则不能删除
        if(dinnerTableByTableId.getTableStatus() == 1){
            //餐桌已被预约
            return ServletConstant.jump("已被预约无法删除","dinnertable?method=findDinnerTableByName&tableName=");
        }

        dinnerTableService.deleteDinnerTableByTableId(tableId);
        return ServletConstant.jump("删除成功", "dinnertable?method=findDinnerTableByName&tableName=");
    }

    /**
     * 添加餐桌
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String addDinnerTable(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String tableName = request.getParameter("tableName");
        if (tableName.isEmpty() || tableName == null) {
            return ServletConstant.jump("名字非法", "backend/detail/table/table-save.jsp");
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "请先登陆";
        }

        //判断是否重名
        List<DinnerTable> dinnerTableByName = dinnerTableService.findDinnerTableByName(tableName.trim());
        for (DinnerTable dinnerTable : dinnerTableByName) {
            if (dinnerTable.getTableName().trim().equals(tableName.trim())){
                return ServletConstant.jump("名字重复", "backend/detail/table/table-save.jsp");
            }
        }

        DinnerTable dinnerTable = new DinnerTable();
        dinnerTable.setTableName(tableName.trim());
        dinnerTable.setTableCreateUserId(user.getUserId());
        dinnerTableService.addDinnerTable(dinnerTable);
        return ServletConstant.jump("添加成功", "dinnertable?method=findDinnerTableByName&tableName=");
    }


}